package com.wjw.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjw.entity.Collect;
import com.wjw.dao.CollectMapper;

/**
 * @author WuJianWei
 * @date 2022/2/28 20:00
 */
@Service
public class CollectService extends ServiceImpl<CollectMapper, Collect> {
    public List<Collect> getCollect(int userId) {
        LambdaQueryWrapper<Collect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Collect::getUserId, userId);
        List<Collect> collects = baseMapper.selectList(wrapper);
        return collects;
    }

    public int findCollect(int userId, int productId) {
        LambdaQueryWrapper<Collect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Collect::getUserId, userId);
        wrapper.eq(Collect::getProductId, productId);
        Collect collect = baseMapper.selectOne(wrapper);
        if (collect == null){
            return 0;
        }
        return 1;
    }

    public void addCollect(int userId, int productId){
        baseMapper.addCollect(userId,productId);
    }
    public void deleteCollect(int userId, int productId){
        LambdaQueryWrapper<Collect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Collect::getUserId, userId);
        wrapper.eq(Collect::getProductId, productId);
        baseMapper.delete(wrapper);
    }
}
